package cn.jinjimi.app;

import android.test.AndroidTestCase;

import cn.jinjimi.base.util.LogUtil;

import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.manager.CustomsManager;
import cn.jinjimi.app.store.DatabaseManager;

import java.util.Random;

/**
 * Created by Administrator on 2015/3/23.
 */
public class DatabaseTest extends AndroidTestCase {

    DatabaseManager mDbMgr = null;
    CustomsManager mCustomerMgr;

    Random mRand = null;

    int mTestCount = 0;

    String mUserId = null;


    public DatabaseTest() {
        mDbMgr = DatabaseManager.getInstance();
        mUserId = AccountManager.getInstance().getAccount().ssoid;
        mCustomerMgr = CustomsManager.getInstance();
        mRand = new Random();
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        LogUtil.println("setUp...");
    }


    private synchronized void updateMut() {
        String name = "向日葵" + mRand.nextInt(500);

        Custom custom = new Custom();
        custom.name = (name);
        custom.customerId = (mRand.nextLong());
        custom.ssoId = mUserId;
//        mCustomerMgr.addOrUpdateCustom(custom);

    }

    private synchronized void updateSin() {
        int size = mCustomerMgr.getCustomsList().size();
        if (size == 0)
            return;
        Custom custom = mCustomerMgr.getCustomsList().get(mRand.nextInt(size));
        custom.type = (mRand.nextInt(3));
//        mCustomerMgr.addOrUpdateCustom(custom);
    }

    private synchronized void getInfo() {
        LogUtil.println(".... getInfo start");
//        mCustomerMgr.addOrUpdateCustoms(mCustomerMgr.getCustomsList());
    }

    private void doTest() {
        new Thread() {
            public void run() {
                int i = 0;
                while (i < 100) {
                    i++;
                    try {
                        sleep(100);
                    } catch (Exception e) {
                    }
                    int type = mRand.nextInt(500);
                    if (type % 7 == 0) {
                        getInfo();
                    } else if (type % 7 == 1 || type % 7 == 2) {
                        updateSin();
                    } else {
                        updateMut();
                    }
                }
                mTestCount++;
            }
        }.start();
    }


//    @Test
//    public void testGetAndSetInfo() {
//        LogUtil.println("testGetAndSetInfo...");
//        doTest();
//        doTest();
//        doTest();
//        doTest();
//        doTest();
//        while (mTestCount != 5) {
//            try {
//                Thread.sleep(20);
//            } catch (Exception e) {
//            }
//        }
//
//    }

    @Override
    public void tearDown() throws Exception {
        super.tearDown();
        LogUtil.println("tearDown...");
    }
}
